function [] = profplot2(left_mean, left_sem, right_mean, right_sem, left_norm, right_norm, left_ref, right_ref);

% PROFPLOT2 - double-sided version of PROFPLOT
%
%    PROFPLOT2(LMEAN, LSEM, RMEAN, RSEM) creates displays of profile averages
%    superimposed on regions representing profile mean +/- SEM.  Two separate
%    profile sets LMEAN/LSEM and RMEAN/RSEM are plotted, extending across the
%    soma.
%
%    PROFPLOT(..., LNORM, RNORM) superimposes normalized profiles on the
%    regions representing mean +/- SEM.
%
%    PROFPLOT2(..., LREF, RREF) adds reference lines at the specified locations.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: June 7, 2000

% default parameters

edge_space = 0.08;			% space between axes & figure edge
middle_space = 0.04;			% space between axes

edge_color = 'none';			% mean +/- SEM region edge color
face_color = 3*[1 1 1]/4;	% mean +/- SEM region fill color

line_color = [0 0 0];		% mean or norm line color
line_width = 1;				% mean or norm line width
line_style = '-';				% mean or norm line style

ref_color = [0 0 0];			% reference line color
ref_width = 0.5;				% reference line width
ref_style = '-';				% reference line style

% validate arguments

if ((nargin < 4) | (nargin > 8))
   msgbox('Invalid number of arguments', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(left_mean) | isempty(left_mean))
   msgbox('LMEAN must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(left_sem) | isempty(left_sem))
   msgbox('LSEM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (any(size(left_sem) ~= size(left_mean)))
   msgbox('LSEM must be the same size as LMEAN', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(right_mean) | isempty(right_mean))
   msgbox('RMEAN must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(right_sem) | isempty(right_sem))
   msgbox('RSEM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (any(size(right_sem) ~= size(right_mean)))
   msgbox('RSEM must be the same size as RMEAN', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 4)
   left_norm = [];
   right_norm = [];
   left_ref = [];
   right_ref = [];
elseif (~isnumeric(left_norm))
   msgbox('LNORM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 5)
   right_norm = [];
   left_ref = [];
   right_ref = [];
elseif (~isnumeric(right_norm))
   msgbox('RNORM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 6)
   left_ref = [];
   right_ref = [];
elseif (~isnumeric(left_ref))
   msgbox('LREF must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 7)
   right_ref = [];
elseif (~isnumeric(right_ref))
   msgbox('RREF must be a numeric array', 'PROFPLOT Error', 'warn');
   return
end

% create figure & axes

h_fig = figure;
h_left = axes('Parent', h_fig, ...
   'Units', 'normalized', ...
   'Tag', 'left');
h_right = axes('Parent', h_fig, ...
   'Units', 'normalized', ...
   'Tag', 'right');

% create mean +/- SEM regions

for i = 2 : size(left_mean, 2)
   h_patch(i - 1) = patch([left_mean(:, 1); flipud(left_mean(:, 1))], ...
      [left_mean(:, i) - left_sem(:, i); flipud(left_mean(:, i) + left_sem(:, i))], ...
      face_color, 'EdgeColor', edge_color, 'Parent', h_left);
end
for i = 2 : size(right_mean, 2)
   h_patch(i - 1) = patch([right_mean(:, 1); flipud(right_mean(:, 1))], ...
      [right_mean(:, i) - right_sem(:, i); flipud(right_mean(:, i) + right_sem(:, i))], ...
      face_color, 'EdgeColor', edge_color, 'Parent', h_right);
end

% plot norm lines if provided
% otherwise plot mean lines

if (isempty(left_norm))
   for i = 2 : size(left_mean, 2)
      h_line(i - 1) = line(left_mean(:, 1), left_mean(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style, 'Parent', h_left);
   end
else   
   for i = 2 : size(left_norm, 2)
      h_line(i - 1) = line(left_norm(:, 1), left_norm(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style, 'Parent', h_left);
   end
end

if (isempty(right_norm))
   for i = 2 : size(right_mean, 2)
      h_line(i - 1) = line(right_mean(:, 1), right_mean(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style, 'Parent', h_right);
   end
else   
   for i = 2 : size(right_norm, 2)
      h_line(i - 1) = line(right_norm(:, 1), right_norm(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style, 'Parent', h_right);
   end
end

% plot reference lines if given

for i = 2 : 2 : size(left_ref, 1)
   h_ref(i/2) = line(left_ref(i - 1, :), left_ref(i, :), ...
      'Color', ref_color, 'LineWidth', ref_width, 'LineStyle', ref_style, 'Parent', h_left);
end

for i = 2 : 2 : size(right_ref, 1)
   h_ref(i/2) = line(right_ref(i - 1, :), right_ref(i, :), ...
      'Color', ref_color, 'LineWidth', ref_width, 'LineStyle', ref_style, 'Parent', h_right);
end

% rescale Y axes

y_lim = [get(h_left, 'YLim') get(h_right, 'YLim')];
y_lim = [min(y_lim) max(y_lim)];
set(h_left, 'YLim', y_lim);
set(h_right, 'YLim', y_lim);

% resize to match X axes scales

x_left = get(h_left, 'XLim');
x_right = get(h_right, 'XLim');
tick_left = get(h_left, 'XTick');
tick_right = get(h_right, 'XTick');
axes_width = 1.0 - 2*edge_space - middle_space;
left_width = axes_width * diff(x_left) / (diff(x_left) + diff(x_right));
right_width = axes_width - left_width;
set(h_left, 'Position', [edge_space 0.1 left_width 0.8], 'XLim', x_left, 'XTick', tick_left);
set(h_right, 'Position', [(1 - edge_space - right_width) 0.1 right_width 0.8], 'XLim', x_right, 'XTick', tick_right);

% reverse left X direction
% eliminate right Y tick labels

%set(h_left, 'XDir', 'reverse');
set(h_right, 'YTick', []);
return
